﻿using System;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Basics.V1
{
    using LdCms.IBLL.Basics;
    using LdCms.Web.Services;
    /// <summary>
    /// 基础公共部分
    /// 媒体文件管理接口：
    ///     1、获取媒体信息
    ///        GET：basics/media/get?access_token=access_token&mediaid=mediaid
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("basics/media")]
    public class BasicsMediaController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IVMediaService VMediaService;
        public BasicsMediaController(IBaseApiManager BaseApiManager, IVMediaService VMediaService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.VMediaService = VMediaService;
        }

        [HttpGet]
        [ActionName("get")]
        public IActionResult GetMedia(string access_token, string mediaid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(access_token);
                string companyId = MemberAccount.CompanyID;
                string memberId = MemberAccount.MemberID;
                var entity = VMediaService.GetVMediaByMember(SystemID, companyId, memberId, mediaid);
                if (entity == null)
                    return Error(logId, "mediaid invalid！");
                var data = new
                {
                    media_id = entity.MediaID,
                    name = entity.FileName,
                    size = entity.FileSize,
                    url = entity.Url,
                    src = entity.Src
                };
                return Success(logId, "ok", data);
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }

    }
}